package com.app.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.app.common.Result;
import com.app.common.WordsTool;
import com.app.model.AreaModel;
import com.app.model.CategoryModel;
import com.app.model.CityModel;
import com.app.service.AreaService;
import com.app.service.CategoryService;
import com.app.vo.AreaVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用返回
 */
@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private AreaService areaService;

    @Autowired
    private CategoryService categoryService;

    //获取地区列表
//    @GetMapping("/getAreaList")
//    public Result getAreaList() {
//        String[] arrWords = WordsTool.arrWords;
//        List<AreaVo> voList=new ArrayList<>();
//        //对地区voList进行初始化
//        for (String arrWord : arrWords) {
//            AreaVo initAreaVo =new AreaVo();
//            initAreaVo.setWords(arrWord);
//            List<AreaModel> areaInitList =new ArrayList<>();
//            initAreaVo.setAreaList(areaInitList);
//            voList.add(initAreaVo);
//        }
//        QueryWrapper<AreaModel> qw =new QueryWrapper<>();
//        qw.orderByDesc("words");
//        List<AreaModel> list = areaService.list();
//        for (AreaModel areaModel : list) {
//            String area = areaModel.getArea();
//            String pinyin = PinyinUtil.getFirstLetter(area,"");
//            String words = pinyin.substring(0,1);
//            for (AreaVo areaVo : voList) {
//                String voWords = areaVo.getWords();
//                if (words.equals(voWords)){
//                    List<AreaModel> areaList = areaVo.getAreaList();
//                    areaList.add(areaModel);
//                }
//            }
//        }
//        return Result.success(voList);
//    }
    //获取所有商家类型列表
    @GetMapping("/getBusinessTypeList")
    public Result getBusinessTypeList() {
        List<CategoryModel> businessTypeList = categoryService.getBusinessTypeList();
        return Result.success(businessTypeList);
    }

    //获取所有月销售额列表
    @GetMapping("/getMonthlySalesTypeList")
    public Result getMonthlySalesTypeList() {
        List<CategoryModel> monthlySalesTypeList = categoryService.getMonthlySalesTypeList();
        return Result.success(monthlySalesTypeList);
    }

    //获取所有商品类型
    @GetMapping("/getGoodsTypeList")
    public Result getGoodsTypeList() {
        List<CategoryModel> businessTypeList = categoryService.getGoodsTypeList();
        return Result.success(businessTypeList);
    }

    /**
     * 获取订单原因
     *
     * @return {@link Result}
     */
    @GetMapping("/getOrderReason")
    public Result getOrderReason() {
        List<CategoryModel> orderReason = categoryService.getOrderReason();
        return Result.success(orderReason);
    }

    /**
     * 获取区域列表
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/getAreaList")
    public Result<Object> getAreaList() {
        List<AreaVo> areaList = areaService.getAreaList();
        return Result.success(areaList);
    }

    /**
     * 获取城市列表
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/getCityList")
    public Result<Object> getCityList() {
        List<AreaVo> areaList = areaService.getCityList();
        return Result.success(areaList);
    }

    /**
     * 获取县列表
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/getCountyList")
    public Result<Object> getCountyList() {
        List<AreaVo> areaList = areaService.getCountyList();
        return Result.success(areaList);
    }

    /**
     * 需要根据拼音字母进行筛选
     *
     * @param
     * @return
     */
    @GetMapping("list")
    public Result list(@RequestParam(value = "keywords", required = false) String keywords) {
        List<String> words = new ArrayList<>();
        for (char c = 'A'; c <= 'Z'; c++) {
            words.add(String.valueOf(c));
        }
        List<Map<String, Object>> response = new ArrayList<>();

        if (keywords != null && !keywords.equals("")) {
            return Result.success(this.areaService.listByKeyWords(keywords));
        }

        for (String s : words
        ) {
            Map<String, Object> m = new HashMap<>();
            if (!this.areaService.listByWords(s).isEmpty()){
                m.put("word", s);
                m.put("list", this.areaService.listByWords(s));
                response.add(m);
            }
        }
        return Result.success(response);
    }


    @GetMapping("getCityById")
    public Result getCityById(@RequestParam(value = "provinceId") Integer provinceId) {
        return Result.success(this.areaService.getCityById(provinceId));
    }


    @GetMapping("getAreaById")
    public Result getAreaById(@RequestParam(value = "cityId") Integer cityId) {
        return Result.success(this.areaService.getAreaById(cityId));
    }

    /**
     * 查看热门
     *
     * @return {@link Result}
     */
    @GetMapping("/getHotCity")
    public Result getHotCity() {
        return Result.success(areaService.getHotCity());
    }
}
